/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
#![allow(unused_assignments, unused_mut)]

use crate::common::*;

extern "C" {
    static mut sudoers_subsystem_ids: [libc::c_uint; 0];
    fn strchr(_: *const libc::c_char, _: libc::c_int) -> *mut libc::c_char;
    fn fprintf(_: *mut FILE, _: *const libc::c_char, _: ...) -> libc::c_int;

}

#[macro_export]
macro_rules! LOG_INDENT {
    () => {
        (b"    \0" as *const u8 as *const libc::c_char as *mut libc::c_char)
    };
}

#[no_mangle]
pub fn writeln_wrap(
    mut fp: *mut FILE,
    mut line: *mut libc::c_char,
    mut linelen: size_t,
    mut maxlen: size_t,
) -> libc::c_int {
    let mut indent: *mut libc::c_char =
        b"\0" as *const u8 as *const libc::c_char as *mut libc::c_char;
    let mut beg: *mut libc::c_char = line;
    let mut end: *mut libc::c_char = std::ptr::null_mut::<libc::c_char>();
    let mut len: libc::c_int = 0;
    let mut outlen: libc::c_int = 0;
    unsafe {
        debug_decl!(SUDOERS_DEBUG_LOGGING!());

        /*
         * Print out line with word wrap around maxlen characters.
         */
        while linelen > maxlen {
            end = beg.offset(maxlen as isize);
            while end != beg && *end as libc::c_int != ' ' as i32 {
                end = end.offset(-1);
            }
            if beg == end {
                /* Unable to find word break within maxlen, look beyond. */
                end = strchr(beg.offset(maxlen as isize), ' ' as i32);
                if end.is_null() {
                    break; /* no word break */
                }
            }
            len = fprintf(
                fp,
                b"%s%.*s\n\0" as *const u8 as *const libc::c_char,
                indent,
                end.offset_from(beg) as libc::c_long as libc::c_int,
                beg,
            );
            if len < 0 {
                debug_return_int!(-1);
            }
            outlen += len;
            while *end as libc::c_int == ' ' as i32 {
                end = end.offset(1);
            }
            linelen = (linelen as libc::c_ulong)
                .wrapping_sub(end.offset_from(beg) as libc::c_long as libc::c_ulong)
                as size_t as size_t;
            beg = end;
            if *indent.offset(0_isize) as libc::c_int == '\0' as i32 {
                indent = LOG_INDENT!();
                maxlen = (maxlen as libc::c_ulong).wrapping_sub(
                    (::core::mem::size_of::<[libc::c_char; 5]>() as libc::c_ulong)
                        .wrapping_sub(1 as libc::c_ulong),
                ) as size_t as size_t;
            }
        }
        /* Print remainder, if any. */
        if linelen != 0 {
            len = fprintf(
                fp,
                b"%s%s\n\0" as *const u8 as *const libc::c_char,
                indent,
                beg,
            );
            if len < 0 {
                debug_return_int!(-1);
            }
            outlen += len;
        }
        debug_return_int!(outlen);
    } //unsafe
}
